package com.sgb.kjwl.view.ui.activity.manage;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityTradeHonorSearchBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageModule;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.manage.TradeHonorEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.manage.TradeHonorAdapter;
import com.sgb.kjwl.view.widget.DictWindow5;
import com.sgb.kjwl.view.widget.IpPopWindow;
import com.sgb.kjwl.view.widget.PickTwoLevelCityPopWindow;
import com.sgb.kjwl.view.widget.TwoAreaPopWindow;
import com.sgb.kjwl.viewmodel.manage.TradeHonorSearchModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * 搜索行业荣誉
 */
public class TradeHonorSearchActivity extends AppActivity {
    @Inject
    TradeHonorSearchModel viewModel;

    private ActivityTradeHonorSearchBinding binding;
    TradeHonorAdapter tradeHonorAdapter;

    List<IpPickSelectEntity> tradeTypeDatas;//行业分类
    List<IpPickSelectEntity> honorTypeDatas;//荣誉分类
    List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> cityDatas2;//地区

    private IpPopWindow tradeTypeWindow;
    private IpPopWindow honorTypeWindow;
    private PickTwoLevelCityPopWindow cityPopWindow2;
    Map map;
    private String honorTypeId;
    private String tradeTypeId;
    private boolean isChange3;
    private boolean isChange2;
    private boolean isChange1;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseManageComponent.builder().appComponent(appComponent)
                .enterpriseManageModule(new EnterpriseManageModule(this))
                .build()
                .inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, TradeHonorSearchActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_trade_honor_search);
        binding.include.ivBack.setOnClickListener(v -> finish());
        binding.include.tvSearch.setOnClickListener(v -> finish());
        binding.roomLayout.setVisibility(View.GONE);

        binding.tvTradeCategory.setOnClickListener(this);
        binding.tvHonorCategory.setOnClickListener(this);
        binding.tvArea.setOnClickListener(this);

        tradeHonorAdapter = new TradeHonorAdapter(this, null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setAdapter(tradeHonorAdapter);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                viewModel.getHonorList(false, map);
            }

            @Override
            public void onLoadMore() {
                viewModel.getHonorList(true, map);
            }
        });

        binding.include.editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    toSearchHonor();
                }
                return false;
            }
        });

        //行业分类
        tradeTypeWindow = new IpPopWindow(getActivity());
        tradeTypeWindow.setFocusable(false);
        tradeTypeWindow.setOutsideTouchable(true);
        tradeTypeWindow.setOnResultClick((type, key2, key3) -> {

//                tradeTypeId = dictEntity.getId();
//                tradeTypeWindow.setCheckDefaultById(tradeTypeId);
            map.put("industryTypeNo", ((IpPickSelectEntity) type).getValue());
            viewModel.getHonorList(false, map);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange1 = true;
                setTextViewBg(binding.tvTradeCategory, R.color.white, R.mipmap.icon_down_red, R.color.color_main);
//                binding.tvTradeCategory.setText(dictEntity.getName());
            } else {
                isChange1 = false;
                setTextViewBg(binding.tvTradeCategory, R.color.white, R.mipmap.ic_down_arrow_n, R.color.color_tab_two);
                setDrwImage(binding.tvTradeCategory, 2);
            }

        });
        tradeTypeWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (!isChange1) {
                    binding.tvTradeCategory.setTextColor(getResources().getColor(R.color.color_666666));
                    setDrwImage(binding.tvTradeCategory, 2);
                } else {
                    setDrwImage(binding.tvTradeCategory, 1);
                }
            }
        });

        //荣誉分类
        honorTypeWindow = new IpPopWindow(getActivity());
        honorTypeWindow.setFocusable(false);
        honorTypeWindow.setOutsideTouchable(true);
        honorTypeWindow.setOnResultClick((type, key2, key3) -> {
//                honorTypeId = dictEntity.getId();
//                honorTypeWindow.setCheckDefaultById(honorTypeId);
            map.put("honorClassification", ((IpPickSelectEntity) type).getValue());
            viewModel.getHonorList(false, map);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange2 = true;
                setTextViewBg(binding.tvHonorCategory, R.color.white, R.mipmap.icon_down_red, R.color.color_main);
//                binding.tvHonorCategory.setText(dictEntity.getName());
            } else {
                isChange2 = false;
                setTextViewBg(binding.tvHonorCategory, R.color.white, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                setDrwImage(binding.tvHonorCategory, 2);
            }

        });
        honorTypeWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (!isChange2) {
                    binding.tvHonorCategory.setTextColor(getResources().getColor(R.color.color_666666));
                    setDrwImage(binding.tvHonorCategory, 2);
                } else {
                    setDrwImage(binding.tvHonorCategory, 1);
                }
            }
        });

        cityPopWindow2 = new PickTwoLevelCityPopWindow(getActivity());
        cityPopWindow2.setFocusable(false);
        cityPopWindow2.setOutsideTouchable(true);
        cityPopWindow2.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                map.put("provinceNo", key1);
                map.put("cityNo", key2);
                viewModel.getHonorList(false, map);
                if (!key3.equals("不限")) {
                    isChange3 = true;
                    setTextViewBg(binding.tvArea, R.color.white, R.mipmap.icon_down_red, R.color.color_main);
//                    binding.tvArea.setText((String) key3);
                } else {
                    isChange3 = false;
                    setTextViewBg(binding.tvArea, R.color.white, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    setDrwImage(binding.tvArea, 2);
                }
            }
        });
        cityPopWindow2.setOnDisMissClick(() -> {
            if (!isChange3) {
                binding.tvArea.setTextColor(getResources().getColor(R.color.color_666666));
                setDrwImage(binding.tvArea, 2);
            } else {
                setDrwImage(binding.tvArea, 1);
            }
        });
    }

    @Override
    protected void init() {
        map = new HashMap();
        tradeTypeDatas = new ArrayList<>();
        honorTypeDatas = new ArrayList<>();
        cityDatas2 = new ArrayList<>();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_trade_category://行业分类
                if (!ClickUtils.isDoubleClick()) {
//                    dismissWindow();
                    if (!isChange1) {
                        if (!tradeTypeWindow.isShowing()) {
                            binding.tvTradeCategory.setTextColor(getResources().getColor(R.color.new_blue));
                            setDrwImage(binding.tvTradeCategory, 0);
                        }
                    } else {
                        if (!tradeTypeWindow.isShowing()) {
                            setDrwImage(binding.tvTradeCategory, 0);
                        }
                    }
                    if (null != tradeTypeDatas && tradeTypeDatas.size() > 0) {
                        tradeTypeWindow.showPopwindow(binding.roomLayout, 0, 0);
//                        tradeTypeWindow.setCheckDefaultById(tradeTypeId);
                    } else {
                        viewModel.getTradeTypes();
                    }
                }
                break;
            case R.id.tv_honor_category:
                if (!ClickUtils.isDoubleClick()) {
//                    dismissWindow();
                    if (!isChange2) {
                        if (!honorTypeWindow.isShowing()) {
                            binding.tvHonorCategory.setTextColor(getResources().getColor(R.color.new_blue));
                            setDrwImage(binding.tvHonorCategory, 0);
                        }
                    } else {
                        if (!honorTypeWindow.isShowing()) {
                            setDrwImage(binding.tvHonorCategory, 0);
                        }
                    }
                    if (null != honorTypeDatas && honorTypeDatas.size() > 0) {
                        honorTypeWindow.showPopwindow(binding.roomLayout, 0, 0);
//                        honorTypeWindow.setCheckDefaultById(honorTypeId);
                    } else {
//                        viewModel.getHonorTypes();
                    }
                }
                break;
            case R.id.tv_area:
                if (!ClickUtils.isDoubleClick()) {
//                    dismissWindow();
                    if (!isChange3) {
                        if (!cityPopWindow2.isShowing()) {
                            binding.tvArea.setTextColor(getResources().getColor(R.color.new_blue));
                            setDrwImage(binding.tvArea, 0);
                        }
                    } else {
                        if (!cityPopWindow2.isShowing()) {
                            setDrwImage(binding.tvArea, 0);
                        }
                    }
                    if (null != cityDatas2 && cityDatas2.size() > 0) {
                        cityPopWindow2.showPopwindow(binding.roomLayout, 0, 0);
                    } else {
                        viewModel.getServiceCity();
                    }
                }
                break;
        }
    }


    /**
     * 搜索荣誉
     */
    private void toSearchHonor() {

        String keyword = binding.include.editSearch.getText().toString().trim();
        map.put("honorName", keyword);
        viewModel.getHonorList(false, map);

    }


    public void showHonorList(List<TradeHonorEntity> list, boolean isLoadMore) {
        binding.recyclerView.refreshComplete();
        binding.roomLayout.setVisibility(View.VISIBLE);
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                tradeHonorAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                tradeHonorAdapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                tradeHonorAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.emptyView.findViewById(R.id.no_data_tv).setVisibility(View.GONE);
            }
        }
    }

    public void showTradeTypePopWindow2(List<IpPickSelectEntity> datas) {
        this.tradeTypeDatas = datas;
        if (tradeTypeId == null || tradeTypeId.equals("")) {
            tradeTypeId = datas.get(0).getId();
        }
        tradeTypeWindow.setData(datas);
//        tradeTypeWindow.setCheckDefaultById(tradeTypeId);
        tradeTypeWindow.showPopwindow(binding.roomLayout, 0, 0);
        tradeTypeWindow.setData(datas);
    }


    public void showHonorTypePopWindow2(List<IpPickSelectEntity> datas) {
        this.honorTypeDatas = datas;
        if (honorTypeId == null || honorTypeId.equals("")) {
            honorTypeId = datas.get(0).getId();
        }
//        honorTypeWindow.setCheckDefaultById(honorTypeId);
        honorTypeWindow.showPopwindow(binding.roomLayout, 0, 0);
        honorTypeWindow.setData(datas);
//        honorTypeWindow.setCheckDefaultById(honorTypeId);
        honorTypeWindow.showPopwindow(binding.roomLayout, 0, 0);
    }


    public void showcityPopWindow2(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> cityDatas) {
        this.cityDatas2 = cityDatas;
        cityPopWindow2.showPopwindow(binding.roomLayout, 0, 0);
        cityPopWindow2.setData(cityDatas, false);
    }


    private void dismissWindow() {
        if (tradeTypeWindow.isShowing()) {
            tradeTypeWindow.dismiss();
        }
        if (honorTypeWindow.isShowing()) {
            honorTypeWindow.dismiss();
        }
        if (cityPopWindow2.isShowing()) {
            cityPopWindow2.dismiss();
        }
    }

    private void setDrwImage(TextView tv, int type) {
        Drawable img_off;
        Resources res = getResources();
        if (type == 0) {
            img_off = res.getDrawable(R.mipmap.icon_up_red);
        } else if (type == 1) {
            img_off = res.getDrawable(R.mipmap.icon_down_red);
        } else {
            img_off = res.getDrawable(R.mipmap.ic_down_arrow_n);
        }
        img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
        tv.setCompoundDrawables(null, null, img_off, null); //设置右图标
    }

    private void setTextViewBg(TextView textView, int bg, int img, int textColor) {
        textView.setBackground(getResources().getDrawable(bg));
        Drawable drawable_n = getResources().getDrawable(img);
        drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable_n, null);
        textView.setTextColor(getResources().getColor(textColor));
    }
}
